﻿using CPPEI.Coolzon.Metadata;
using CPPEI.Coolzon.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.BrowserApp.Views
{
    /// <summary>
    /// Interaction logic for MainView.xaml
    /// </summary>
    public partial class MainView : CPPEI.Coolzon.UI.View
    {
        /// <summary>
        /// 当前展示的方案
        /// </summary>
        private Metadata.Solution _solution;

        private UI.Views.RenderView _ctlRenderView;

        public bool ExpandFlag = true;

        public static readonly DependencyProperty ColumnWidthProperty =
           DependencyProperty.Register("ColumnWidth", typeof(double), typeof(MainView),
           new PropertyMetadata(double.MinValue, new PropertyChangedCallback(MainView.OnColumnWidthPropertyChanged)));

        public double ColumnWidth
        {
            get
            {
                return (double)GetValue(ColumnWidthProperty);
            }
            set
            {
                SetValue(ColumnWidthProperty, value);
            }
        }
        public MainView(Metadata.Solution solution)
        {
            InitializeComponent();

            _solution = solution;

            // 从uicontext中获取renderview对象
            _ctlRenderView = UIContext.Current.RenderView as CPPEI.Coolzon.UI.Views.RenderView;
            _ctlRenderView.SetDisplayMode(PartFrameDisplayMode.HStretch);
            recentVisit._solution = _solution;
            _ctlRenderViewBorder.Child = _ctlRenderView;
            _ctlRenderView.IsShowScrollBar = true;

            //LocalEventProxy.Instance.SetRenderView(_ctlRenderView);
            RemoteEventProxy.Instance.SetRenderView(_ctlRenderView);

            // 注册到事件总线上
            //Common.EventBus.Instance.Register(LocalEventProxy.Instance);
            Common.EventBus.Instance.Register(RemoteEventProxy.Instance);

            Event.Core.CoreEventBus.Register(RemoteEventProxy.Instance);
            //Common.EventBus.Instance.Register(sceneImmediate);
            //Common.EventBus.Instance.Register(recentVisit);
            Event.Core.CoreEventBus.Register(sceneImmediate);
            Event.Core.CoreEventBus.Register(recentVisit);
        }

        protected override void OnActive()
        {
            InitControl();
            base.OnActive();
        }
        private void InitControl()
        {
            sceneNavigation.solution = _solution;
            sceneNavigation.BindScene();

            Scene sceneFirst = _solution.Scenes.First();
            sceneImmediate.Scenes = sceneFirst.Scenes;
            sceneImmediate.BindSceneImmediate();

            _ctlRenderView.SwitchToScene(sceneFirst.Name, null);
        }
        private void expandImg_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Storyboard storyBoard = new Storyboard();
            DoubleAnimation widthAnimation = new DoubleAnimation();
            if (ExpandFlag)
            {
                expandImg.Style = (Style)gridLayout.Resources["Close"];
                ExpandFlag = false;

                widthAnimation.From = 161;
                widthAnimation.To = 0;
            }
            else
            {
                ExpandFlag = true;
                expandImg.Style = (Style)gridLayout.Resources["Expand"];

                widthAnimation.From = 0;
                widthAnimation.To = 161;
            }
            widthAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.4));
            Storyboard.SetTarget(widthAnimation, this);
            Storyboard.SetTargetProperty(widthAnimation, new PropertyPath(ColumnWidthProperty));
            storyBoard.Children.Add(widthAnimation);
            storyBoard.Begin();
        }
        private static void OnColumnWidthPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            MainView mv = d as MainView;
            mv._ctlGridCenter.ColumnDefinitions[0].Width = new GridLength((double)e.NewValue);

        }
    }
}
